package com.easylinkin.linkappapi.grid.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.grid.entity.GridSecurityLog;
import com.easylinkin.linkappapi.grid.models.dto.GridSecurityLogDayDTO;
import com.easylinkin.linkappapi.grid.models.query.GridSecurityLogQuery;
import com.easylinkin.linkappapi.grid.service.IGridSecurityLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;


/**
 * 网格安全员日志
 *
 * @author xy
 * @since 2025-06-30
 */
@Api(tags = "网格安全员日志")
@RestController
@RequestMapping("/gird/gridSecurityLog")
public class GridSecurityLogController {
    @Autowired
    private IGridSecurityLogService gridSecurityLogService;

    /**
     * 查所有的检查项
     *
     * @return
     */
    @ApiOperation(value="查询所有的检查项", notes="查询所有的检查项")
    @GetMapping(value = "/queryAllCheckItems")
    public RestMessage queryAllCheckItems() {
        return RestBuilders.successBuilder().data(gridSecurityLogService.queryAllCheckItems()).build();
    }

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value="分页列表查询", notes="分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<GridSecurityLogQuery> requestModel) {
        // Assert.notNull(requestModel,"参数不能为空");
        // Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<GridSecurityLog> pageList = gridSecurityLogService.queryPage(requestModel);
        return RestBuilders.successBuilder().data(pageList).build();
    }

    /**
     * 按月查询日志
     *
     * @param month 月份 格式：yyyy-MM
     * @return
     */
    @ApiOperation(value="按月查询日志", notes="按月查询日志")
    @GetMapping(value = "/queryByMonth")
    public RestMessage queryByMonth(@RequestParam("month") String month) {
        List<GridSecurityLogDayDTO> list = gridSecurityLogService.queryByMonth(month);
        return RestBuilders.successBuilder().data(list).build();
    }
    
    /**
     * 按月导出日志
     *
     * @param month 月份 格式：yyyy-MM
     * @param projectName 项目名称
     * @return
     */
    @ApiOperation(value="按月导出日志", notes="按月导出日志")
    @GetMapping(value = "/exportByMonth")
    public void exportByMonth(@RequestParam("month") String month,
                             HttpServletResponse response) throws IOException {
        gridSecurityLogService.exportByMonth(month, response);
    }

    /**
     * 校验网格点某天是否有日志记录
     *
     * @param gridId
     * @param logTime
     * @return
     */
    @ApiOperation(value="校验网格点某天是否有日志记录", notes="校验网格点某天是否有日志记录")
    @GetMapping(value = "/checkGridLog")
    public RestMessage checkGridLog(@RequestParam("gridId") Long gridId, @RequestParam("logTime") String logTime) {
        boolean hasLog = gridSecurityLogService.checkGridLog(gridId, logTime);
        return RestBuilders.successBuilder().data(hasLog).build();
    }

    /**
     * 保存
     *
     * @param gridSecurityLog
     * @return
     */
    @ApiOperation(value="保存", notes="保存")
    @PostMapping(value = "/save")
    public RestMessage save(@RequestBody GridSecurityLog gridSecurityLog) {
        gridSecurityLogService.saveOrUpdateLog(gridSecurityLog);
        return RestBuilders.successBuilder().message("保存成功" ).build();
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value="通过id删除", notes="通过id删除")
    @DeleteMapping(value = "/delete")
    public RestMessage delete(@RequestParam("id") Integer id) {
        gridSecurityLogService.removeById(id);
        return RestBuilders.successBuilder().message("删除成功").build();
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public RestMessage deleteBatch(@RequestParam("ids") String ids) {
        this.gridSecurityLogService.removeByIds(Arrays.asList(ids.split(",")));
        return RestBuilders.successBuilder().message("批量删除成功").build();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public RestMessage queryById(@RequestParam("id") Long id) {
        GridSecurityLog gridSecurityLog = gridSecurityLogService.queryById(id);
        if(gridSecurityLog == null) {
            return RestBuilders.errorBuilder().message("未找到对应数据").build();
        }
        return RestBuilders.successBuilder().data(gridSecurityLog).build();
    }
}
